package com.reactim.message.controller;

import com.reactim.message.dto.Result;
import com.reactim.message.dto.response.ConversationResponse;
import com.reactim.message.service.ConversationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 会话管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/conversations")
@RequiredArgsConstructor
public class ConversationController {

    private final ConversationService conversationService;

    /**
     * 获取会话列表
     */
    @GetMapping
    public Mono<Result<ConversationListResponse>> getConversations(@RequestParam(required = false) String username) {
        // 简化处理，实际应该从认证信息中获取用户名
        final String finalUsername = username != null ? username : "defaultUser";
        return conversationService.getConversationsByUser(finalUsername)
                .map(conversations -> {
                    ConversationListResponse response = new ConversationListResponse();
                    response.setConversations(conversations);
                    response.setTotal(conversations.size());
                    return Result.success(response);
                })
                .doOnSuccess(result -> log.debug("获取会话列表: {}", finalUsername));
    }

    /**
     * 获取指定会话信息
     */
    @GetMapping("/{conversationId}")
    public Mono<Result<ConversationResponse>> getConversation(@PathVariable String conversationId) {
        return conversationService.getConversationById(conversationId)
                .map(Result::success)
                .doOnSuccess(result -> log.debug("获取会话信息: {}", conversationId));
    }

    /**
     * 创建会话
     */
    @PostMapping
    public Mono<Result<ConversationResponse>> createConversation(@RequestBody CreateConversationRequest request, @RequestParam(required = false) String username) {
        if (username == null) {
            username = "defaultUser";
        }
        return conversationService.createConversation(username, request)
                .map(Result::success)
                .doOnSuccess(result -> log.info("创建会话: {}", request.getName()));
    }

    // DTO classes
    public static class ConversationListResponse {
        private List<ConversationResponse> conversations;
        private Integer total;
        
        public List<ConversationResponse> getConversations() { return conversations; }
        public void setConversations(List<ConversationResponse> conversations) { this.conversations = conversations; }
        public Integer getTotal() { return total; }
        public void setTotal(Integer total) { this.total = total; }
    }

    public static class CreateConversationRequest {
        private String name;
        private String type; // "private" or "group"
        private List<Long> participants;
        
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getType() { return type; }
        public void setType(String type) { this.type = type; }
        public List<Long> getParticipants() { return participants; }
        public void setParticipants(List<Long> participants) { this.participants = participants; }
    }
}